<?php
// 导入基础类
require_once(dirname(__FILE__) . '/base.class.php');
class Product extends Base
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        商品列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        if (isset($_GET['cid'])) {
            $cid = $_GET['cid'];
        }

        $sql = "select id,name,cover,price,description,market_price,brand,category,sales_volume,status,hits from products";
        if (isset($cid)) {
            $sql .= " where cid={$cid}";
        }
        $sql .=" order by id asc ";
        $sql .= " limit {$start},{$page_num}";
       
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将商品图片处理成绝对路径
            foreach ($list as $key => $item) {
                $list[$key]['cover'] = path($item['cover']);
            }
            // 获取总记录数
            $sql = "select count(id) as count from products";
            if (isset($cid)) {
                $sql .= " where cid={$cid}";
            }
            $res = $this->db->get_one($sql);
            $total = $res['count'];
            $output['data'] = ['list' => $list, 'total' => $total];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取商品失败';
        }

        echo json_encode($output);
    }

    /*
        新增商品
        insert into products set cover="http://vue-admin-server.duans.top/uploads/20200713/thumb_5f0c20ed64d4e.jpg",name="测试商品",price="888",market_price="1999",sales_volume="888",cid="1",bid="1",description="测试商品",introduce="测试商品",status="1",brand="小米MI",category="热门推荐"
    */
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $data = $_POST;
            $data['brand'] = $this->get_brand_by_id($data['bid']);
            $data['category'] = $this->get_cate_by_id($data['cid']);
            $data['introduce'] = htmlspecialchars($data['introduce']);
            $queryStr = arr2str($data);
            $sql = "insert into products set {$queryStr}";
            // $output['sql']=$sql;
            $res = $this->db->query($sql);
            if (!$res) {
                $output['errcode'] = 1;
                $output['msg'] = '操作失败';
            }
            echo  json_encode($output);
        }
    }

    /*
        根据id获取品牌名
    */
    private function get_brand_by_id($id)
    {
        $data = $this->db->get_one("select * from brands where id={$id} limit 1");
        return $data['brand_name'];
    }
    /*
        根据id获取分类名称
    */
    private function get_cate_by_id($id)
    {
        $data = $this->db->get_one("select * from categories where id={$id} limit 1");
        return $data['cate_name'];
    }

    /*
        更新商品信息
    */
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            $id = $_GET['id'];
            $data = $_POST;
            unset($data['id']);
            if (isset($data['createtime'])) {
                $data['createtime'] = date('Y-m-d H:i:s');
            }
            if (isset($data['introduce'])) {
                $data['introduce'] = htmlspecialchars($data['introduce']);
            }
            if (isset($data['bid'])) {
                $data['brand'] = $this->get_brand_by_id($data['bid']);
            }
            if (isset($data['cid'])) {
                $data['category'] = $this->get_cate_by_id($data['cid']);
            }
            // 将html标签转换为html实体
            $queryStr = arr2str($data);
            $sql = "update products set {$queryStr} where id={$id}";
            $res = $this->db->query($sql);
            $output['sql'] = $sql;
            if (!$res) {
                $output['errcode'] = 1;
                $output['msg'] = '操作失败';
            }
            echo  json_encode($output);
        }
    }

    /*
        获取商品详情
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($id = $_GET['id']) {
            $sql = "select * from products where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['colors'] = explode('|', $res['colors']);
                $res['versions'] = explode('|', $res['versions']);
                $res['imgs'] = explode('|', $res['imgs']);
                foreach ($res['imgs'] as $key => $val) {
                    $res['imgs'][$key] = path($val);
                }
                $res['cover'] = path($res['cover']);
                $res['introduce'] = htmlspecialchars_decode($res['introduce']);
                $output['data'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '商品不存在';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = 'params error';
        }
        echo json_encode($output);
    }

    /*
        商品搜索
    */
    function search()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($keywords = $_GET['keywords']) {
            $sql = "select id,name,cover,price,market_price,description,brand,category,sales_volume from products where name like '%{$keywords}%'";
            $list = $this->db->query($sql);
            if (!empty($list)) {
                $output['data'] = path($list, 'cover');
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '暂无搜索结果';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
    /*
        删除商品
    */
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            if ($id > 69) {
                $res = $this->db->query("delete from products where id={$id}");
                if (!$res) {
                    $output['errcode'] = 1;
                    $output['msg'] = 'fail';
                }
            }
            echo json_encode($output);
        }
    }
}
